package nccloud.web.ifm.apply.handler;

import java.util.List;
import nc.itf.fi.rateconfig.IRateconfigQueryService;
import nc.vo.ifm.DividendMethodEnum;
import nc.vo.ifm.EvenEnum;
import nc.vo.ifm.IncomeDiviMethodEnum;
import nc.vo.ifm.LimitPropertyEnum;
import nc.vo.ifm.ProductStatusEnum;
import nc.vo.ifm.RedeemMethodEnum;
import nc.vo.ifm.RiskEnum;
import nc.vo.ifm.apply.AggInvestApplyVO;
import nc.vo.ifm.apply.InvestApplyVO;
import nc.vo.imf.constants.TMIMFConst;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.ClientInfo;
import nccloud.framework.web.container.SessionContext;
import nccloud.framework.web.convert.translate.Translator;
import nccloud.framework.web.processor.template.BillCardConvertProcessor;
import nccloud.framework.web.ui.model.GridModel;
import nccloud.framework.web.ui.model.row.Cell;
import nccloud.framework.web.ui.model.row.Row;
import nccloud.framework.web.ui.pattern.billcard.BillCard;
import nccloud.framework.web.ui.pattern.billcard.BillCardFormulaHandler;
import nccloud.framework.web.ui.pattern.billcard.CardHeadAfterEditEvent;
import nccloud.framework.web.ui.pattern.form.Form;
import nccloud.impl.platform.common.util.StringUtils;
import nccloud.web.ifm.investapply.util.ApplyPubUtil;
import nccloud.web.ifm.investapply.util.ApplyQueryUtil;
import nccloud.web.tmpub.afteredit.bean.UIProp;
import nccloud.web.tmpub.handler.AbstractCommonAfterEditHandler;
import nccloud.web.tmpub.util.NCCFrontPrecisionUtil;

public class ApplyOrgChangeEditAfterHandler extends AbstractCommonAfterEditHandler<CardHeadAfterEditEvent, BillCard>
{
  protected BillCard processAfterEdit(CardHeadAfterEditEvent event, UIProp uiProp)
    throws BusinessException
  {
    BillCard card = event.getCard();
    BillCardConvertProcessor processor = new BillCardConvertProcessor();
    AggInvestApplyVO vo = (AggInvestApplyVO)processor.fromBillCard(card);
    loadBaseInforByOrg(vo);
    card = doReturn(vo);
    return card;
  }

  protected AggInvestApplyVO loadBaseInforByOrg(AggInvestApplyVO vo)
    throws BusinessException
  {
    String pk_org = vo.getParentVO().getPk_org();
    if (StringUtils.isBlank(pk_org)) {
      return vo;
    }
    setApplyDefaultValue(vo);
    return vo;
  }

  protected void setApplyDefaultValue(AggInvestApplyVO vo)
    throws BusinessException
  {
    InvestApplyVO pvo = vo.getParentVO();
    InvestApplyVO newvo = new InvestApplyVO();
    newvo.setPk_org(pvo.getPk_org());
    UFDate billmakedate = new UFDate(SessionContext.getInstance().getClientInfo().getBizDateTime());
    if (StringUtils.isBlank(pvo.getPk_apply())) {
      String billmaker = SessionContext.getInstance().getClientInfo().getUserid();
      newvo.setBillmaker(billmaker);
      newvo.setBillmakedate(billmakedate);
      newvo.setPk_billtypecode(getBillTypeCode());
    }

    newvo.setInterestday("1");
    newvo.setVbillstatus(Integer.valueOf(-1));
    newvo.setBillstatus(Integer.valueOf(0));
    newvo.setVdef5(pvo.getVdef5());
   
    newvo.setProductstatus(ProductStatusEnum.APPLYING.toStringValue());
    
    
    newvo.setBoundary(Integer.valueOf(1));
    newvo.setPaytype(Integer.valueOf(1));
    newvo.setPk_group(ApplyQueryUtil.getGroupByOrg(pvo.getPk_org()));
    newvo.setPk_olccurr(ApplyQueryUtil.getOrgStandardCurrtype(pvo.getPk_org()));
    newvo.setPk_currtype(ApplyQueryUtil.getOrgStandardCurrtype(pvo.getPk_org()));
    newvo = (InvestApplyVO)ApplyQueryUtil.processPrecision(newvo, true, getBusiDate());

    newvo.setLimit_property(LimitPropertyEnum.FIXED_LIMIT.toStringValue());

    newvo.setEventype(Integer.valueOf(EvenEnum.NONGUA_MAYBE_PROFIT.toIntValue()));

    newvo.setRisk(Integer.valueOf(RiskEnum.R1.toIntValue()));

    newvo.setPurchasedate(billmakedate);
  

    newvo.setRedeemmethod(Integer.valueOf(RedeemMethodEnum.MONEYREDEEM.toIntValue()));

    newvo.setDividendmethod(Integer.valueOf(DividendMethodEnum.BONDDIVID.toIntValue()));

    newvo.setIncomedivimethod(Integer.valueOf(IncomeDiviMethodEnum.ONEPAYMENT.toIntValue()));
    
    
    newvo.setSettleaccount(pvo.getSettleaccount());

    vo.setParentVO(newvo);
  }

  protected BillCard doReturn(AggInvestApplyVO vo)
  {
    BillCardConvertProcessor processor = new BillCardConvertProcessor();
    BillCard billCard = processor.convert(TMIMFConst.CONST_PAGECODE_ADJUST_CARD, vo);

    Translator translator = new Translator();
    translator.translate(billCard);

    BillCardFormulaHandler handler = new BillCardFormulaHandler(billCard);
    handler.handleLoadFormula();

    List headPrecisionFields = ApplyPubUtil.getHeadPrecisionFields();
    try {
      NCCFrontPrecisionUtil.processBillCardPrecision(billCard, headPrecisionFields, null);
    }
    catch (BusinessException e) {
      ExceptionUtils.wrapException(e);
    }

    int ratedigit = getRateDigit();
    Form form = billCard.getHead();
    if (form == null) {
      return billCard;
    }
    GridModel grid = form.getModel();
    if (grid == null) {
      return billCard;
    }
    List<Row> rows = grid.getListRows();
    if ((rows != null) && (rows.size() > 0)) {
      for (Row r : rows) {
        Cell cell = r.getCell("expectedrate");
        if (cell != null) {
          cell.setScale(ratedigit);
        }
      }
    }
    return billCard;
  }

  private int getRateDigit() {
    IRateconfigQueryService service = (IRateconfigQueryService)ServiceLocator.find(IRateconfigQueryService.class);
    int digits = 0;
    try {
      digits = service.queryRateSysParaInt().intValue();
    } catch (BusinessException e) {
      ExceptionUtils.wrapException(e);
    }
    return digits;
  }

  protected String getBillTypeCode()
  {
    return TMIMFConst.CONST_BILLTYPE_APPLY;
  }

  private UFDate getBusiDate()
  {
    return new UFDate(SessionContext.getInstance().getClientInfo().getBizDateTime());
  }
}